using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;


namespace _395_2d
{
    public class StartScreen : Microsoft.Xna.Framework.DrawableGameComponent, IPublisher
    {
        SpriteBatch spriteBatch;
        SpriteFont font;
        Texture2D image;
        string text;

        ArrayList subscribers;

        public StartScreen(Game game)
            : base(game)
        {
            text = "Press ";
            text += (GamePad.GetState(PlayerIndex.One).IsConnected) ? "Start " : "Enter ";
            text += "to Continue";

            subscribers = new ArrayList();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>("HUDFont");
            image = Game.Content.Load<Texture2D>("welcomeScreen");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Start) ||
               Keyboard.GetState().IsKeyDown(Keys.Enter))
            {
                foreach (ISubscriber s in subscribers) {
                    s.Deliver(new StartGameMessage(gameTime));
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(image, new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(font, text, new Vector2(300, 340), Color.Red);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void Subscribe(ISubscriber s)
        {
            subscribers.Add(s);
        }
    }
}
